package global_error_handler

import (
	"galaxy-manage/internal/code"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"runtime/debug"
)

func Recovery() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if r := recover(); r != nil {
				// 记录日志
				log.Printf("panic recovered in %v: %v\n", c.Request.URL.Path, r)
				// 打印堆栈跟踪（可选）
				log.Printf("Stack: %s", debug.Stack())
				// 获取异常
				decodeErr := code.DecodeErr(r)
				c.JSON(http.StatusOK, decodeErr)
			}
		}()
		// 继续执行后续的Handler
		c.Next()
	}
}
